﻿
using BulletSharp;
using ET.HelloWorld.Hotfix.Entitys;
using ETModel;
using UnityEngine;

namespace ETHotfix {
    [ObjectSystem]
    public class GameSceneStart : StartSystem<HotfixComponent> {
        public override void Start(HotfixComponent self) {
            if (StartConfigComponent.Instance.StartConfig.AppType.HasFlag(AppType.Map)) {
                GameSceneRes.Start();
            }
        }
    }
    [ObjectSystem]
    public class GameSceneDestroy : DestroySystem<HotfixComponent> {
        public override void Destroy(HotfixComponent self) {
            if (StartConfigComponent.Instance.StartConfig.AppType.HasFlag(AppType.Map)) {
                GameSceneRes.Destroy();
            }
        }
    }
    /// <summary>
    /// 场景资源,只有Map服务器可用
    /// </summary>
    public static class GameSceneRes {
        public static GameUnit Ground;
        public static void Start() {
            UnitConfig unitConfig = Game.Scene.GetComponent<ConfigComponent>().Get<UnitConfig>(1002);
            Log.Debug($"测试获取一个单位配置 Id:{unitConfig.Id} {JsonHelper.ToJson(unitConfig)} ");

            //把地板加载出来
            if (Ground == null) {

                var groundCfg = Game.Scene.GetComponent<ConfigComponent>().Get<UnitConfig>(5001);

                var pos = new Vector3(0, -0.001f, 0);

                Ground = ComponentFactory.CreateWithId<GameUnit, long>(IdGenerater.GenerateId(), 0);
                Ground.UnitConfigId = groundCfg.Id;
                Ground.Center = groundCfg.GetCenter();
                Ground.SetPosition(pos);
                //地图的地形作为物理形状
                Ground.AddComponent<BulletCollisionShape, CollisionShape>(groundCfg.CreateBulletCollisionShape());
                Ground.AddComponent<BulletCollisionObject, float, Vector3, Quaternion>(0, Ground.GetPosition(), Ground.GetRotation());

                //加入到场景里去
                Game.Scene.GetComponent<UnitsComponent>().Add(Ground);


                //将地形加入客户端调试线框
                Game.Scene.GetComponent<DebugInfoSyncComponent>()?.UpdateGizmosMeshSync(Ground.Id, Ground.BuildUnitInfo());

            }
        }
        public static void Destroy() {
            if (Ground != null) {
                Ground.Dispose();
                Ground = null;
            }
        }
    }
}
